package de.homer.server.setup;

import java.util.Set;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import de.homer.adapter.domain.PluginMetaInformation;
import de.homer.persistence.adapter.PluginRepository;
import de.homer.server.service.PluginScanner;

/**
 * @author André Stannek
 * 
 */
@Component
public class StartupPluginScanner implements InitializingBean {

	/**
	 * 
	 */
	@Autowired
	private PluginScanner scanner;

	/**
	 * 
	 */
	@Autowired
	private PluginRepository pluginRepository;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@Override
	public final void afterPropertiesSet() throws Exception {
		Set<PluginMetaInformation> foundPlugins = scanner.findPlugins();
		for (PluginMetaInformation pluginDescriptor : foundPlugins) {
			Logger.getLogger("PluginClassLoading").log(Level.INFO,
					"Found Plugin " + pluginDescriptor.getPluginClassName());
			pluginRepository.merge(pluginDescriptor);
		}
	}
}
